<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>10.2</title>
    <style>
        table {
            width: 300px;
            margin: 100px auto;
            border: 1px solid red;
            border-collapse: collapse;
        }

        thead tr {
            background-color: rgb(0, 141, 208);
            line-height: 30px;
            text-align: center;
            font-size: 14px;
        }

        tbody tr {
            line-height: 30px;
            text-align: center;
            font-size: 14px;
        }

        table td,
        th {
            border: 0.5px solid #ccc;
        }
    </style>

</head>

<body>
    <table>
        <thead>
            <tr>
                <th><input type="checkbox" name="" id="hbtn"></th>
                <th>商品</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" name="" id="btn"></td>
                <td>iphone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id="btn"></td>
                <td>ipad Pro</td>
                <td>5000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id="btn"></td>
                <td>ipad Air</td>
                <td>2000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" id="btn"></td>
                <td>Apple Watch</td>
                <td>2000</td>
            </tr>
        </tbody>
    </table>

    <script>
        var hBtn = document.querySelector('#hbtn');

        console.log(hBtn);

        var bBtn = document.querySelectorAll('#btn');
        console.log(bBtn);
        hBtn.onclick = function () {

            for (let index = 0; index < bBtn.length; index++) {
                bBtn[index].checked = this.checked;
            }
        }


        for (let index = 0; index < bBtn.length; index++) {
            
            bBtn[index].onclick = function () {

                var flag = true;
                
                for (let index = 0; index < bBtn.length; index++) {
                    if (!bBtn[index].checked) {
                        flag = false;
                    }
                }
                hBtn.checked = flag;
            }

        }

    </script>

</body>

</html>